package com.appspot.myjsf2;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import oauth.signpost.OAuth;
import oauth.signpost.OAuthConsumer;
import oauth.signpost.OAuthProvider;
import oauth.signpost.basic.DefaultOAuthConsumer;
import oauth.signpost.basic.DefaultOAuthProvider;
import oauth.signpost.exception.OAuthCommunicationException;
import oauth.signpost.exception.OAuthExpectationFailedException;
import oauth.signpost.exception.OAuthMessageSignerException;
import oauth.signpost.exception.OAuthNotAuthorizedException;
import oauth.signpost.signature.SignatureMethod;

public class TwitterAuther {
	/*
	public static OAuthConsumer consumer = new DefaultOAuthConsumer(
            "iIlNngv1KdV6XzNYkoLA",
            "exQ94pBpLXFcyttvLoxU2nrktThrlsj580zjYzmoM",
            SignatureMethod.HMAC_SHA1);
*/
	public static OAuthConsumer consumer = new DefaultOAuthConsumer(
            "0vWjBPrlkEVcjgj1qFbzow",
            "Npf1NDqPUw56WpCZ8SrX7RsqcdxOiFLEsngQnA0sy4",
            SignatureMethod.HMAC_SHA1);

    public static OAuthProvider provider = new DefaultOAuthProvider(consumer,
            "http://twitter.com/oauth/request_token",
            "http://twitter.com/oauth/access_token",
            "http://twitter.com/oauth/authorize");


    public static String getData(String pin) throws OAuthMessageSignerException, OAuthNotAuthorizedException, OAuthExpectationFailedException, OAuthCommunicationException, IOException{
        provider.retrieveAccessToken(pin);

        System.out.println("Access token: " + consumer.getToken());
        System.out.println("Token secret: " + consumer.getTokenSecret());

        URL url = new URL("http://twitter.com/statuses/mentions.xml");
        HttpURLConnection request = (HttpURLConnection) url.openConnection();

        consumer.sign(request);

        System.out.println("Sending request to Twitter...");
        request.connect();

        System.out.println("Response: " + request.getResponseCode() + " "
                + request.getResponseMessage());
        String result = "Response: " + request.getResponseCode() + " "
        + request.getResponseMessage();

        StringBuffer buffer = new StringBuffer();
        BufferedInputStream providerResponseData = new BufferedInputStream(request.getInputStream());
        java.io.InputStreamReader isr = new java.io.InputStreamReader(providerResponseData);
        BufferedReader bb = new BufferedReader(isr);
        String index = "";
        while ((index = bb.readLine()) != null) {
        	buffer.append(index);
        	System.out.println("Data returned");
        }
        
        
    	return result + buffer.toString();

    }
    public static void main(String[] args) throws Exception {

        OAuthConsumer consumer = new DefaultOAuthConsumer(
                "iIlNngv1KdV6XzNYkoLA",
                "exQ94pBpLXFcyttvLoxU2nrktThrlsj580zjYzmoM",
                SignatureMethod.HMAC_SHA1);

        OAuthProvider provider = new DefaultOAuthProvider(consumer,
                "http://twitter.com/oauth/request_token",
                "http://twitter.com/oauth/access_token",
                "http://twitter.com/oauth/authorize");

        System.out.println("Fetching request token from Twitter...");

        // we do not support callbacks, thus pass OOB
        String authUrl = provider.retrieveRequestToken(OAuth.OUT_OF_BAND);

        System.out.println("Request token: " + consumer.getToken());
        System.out.println("Token secret: " + consumer.getTokenSecret());

        System.out.println("Now visit:\n" + authUrl
                + "\n... and grant this app authorization");
        System.out.println("Enter the PIN code and hit ENTER when you're done:");

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String pin = br.readLine();

        System.out.println("Fetching access token from Twitter...");

        provider.retrieveAccessToken(pin);

        System.out.println("Access token: " + consumer.getToken());
        System.out.println("Token secret: " + consumer.getTokenSecret());

        URL url = new URL("http://twitter.com/statuses/mentions.xml");
        HttpURLConnection request = (HttpURLConnection) url.openConnection();

        consumer.sign(request);

        System.out.println("Sending request to Twitter...");
        request.connect();

        System.out.println("Response: " + request.getResponseCode() + " "
                + request.getResponseMessage());
    }
}
